﻿using System.Threading.Tasks;
using System.Windows;


namespace Xyzzer.AsyncUI
{
    partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += MainPage_Loaded;
        }

        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(
                "We welcome you to the world of UI development!",
                "Hello async/await!",
                MessageBoxButton.OK);

            await RunAsyncAwaitVersion();
        }

        //------------------------------------------------------------------------------------
        // Async/Await version (26 lines).
        // Control flow logic visible almost as clearly as if the code was running synchronously.
        private async Task RunAsyncAwaitVersion()
        {
            while (true)
            {
                //await Task.Delay(500);

                var result = await Dialog.Show(
                    "Hello async/await!",
                    "We welcome you to the world of UI development!",
                    "Cool!",
                    "Awesome!");

                //await Task.Delay(500);

                result = await Dialog.Show(
                    "You said",
                    string.Format("\"{0}\"", result),
                    "Repeat",
                    "Exit");

                if (result == "Exit")
                {
                    throw new ExitApplicationException();
                }
            }
        }
    }
}
